package com.okonna.selenium.tide.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.okonna.selenium.tide.Application;

public class SandboxView
{
    private Shell shMain;
    private Display display;
    private Application app;

    public SandboxView(Display tmpDisplay, Application tmpApp)
    {
		System.out.println(this.getClass().getSimpleName() + ".MainView(" + tmpDisplay + ", " + tmpApp + ")");
		display = tmpDisplay;
		shMain = new Shell(display);
		app = tmpApp;
    }

    public Shell initView()
    {
		System.out.println(this.getClass().getSimpleName() + ".initView()");
		shMain.setText("Selenium Tide");
		shMain.setImage(new Image(display, "selenium-rc-logo.png"));
		shMain.setSize(640, 480);
		shMain.setLayout(new GridLayout(2, false));
		
		final TabFolder tabFolder = new TabFolder(shMain, SWT.NONE);
		
		final TabInteractiveView swtTabInteractiveView = new TabInteractiveView(tabFolder, app);
		final Composite cmpInteractive = swtTabInteractiveView.initComposite(SWT.BORDER);
		//tabFolder.setSize(300, 200);
		

	    TabItem tiInteractive = new TabItem (tabFolder, SWT.NULL);
	    tiInteractive.setText ("Interactive");

	    TabItem item2 = new TabItem (tabFolder, SWT.NULL);
	    item2.setText ("Test");
		
		tiInteractive.setControl(cmpInteractive);
	    

		final Menu mnuMain = new Menu(shMain, SWT.BAR);
	
		final Menu mnuFile = new Menu(shMain, SWT.DROP_DOWN);
		
		final Menu mnuEdit = new Menu(shMain, SWT.DROP_DOWN);
		
		final Menu mnuOptions = new Menu(shMain, SWT.DROP_DOWN);
		
		final Menu mnuHelp = new Menu(shMain, SWT.DROP_DOWN);
		
		final MenuItem mniFileHeader = new MenuItem(mnuMain, SWT.CASCADE);
		mniFileHeader.setText("&File");	
		mniFileHeader.setMenu(mnuFile);
		
		final MenuItem mniEditHeader = new MenuItem(mnuMain, SWT.CASCADE);
		mniEditHeader.setText("&Edit");	
		mniEditHeader.setMenu(mnuEdit);
		
		final MenuItem mniOptionsHeader = new MenuItem(mnuMain, SWT.CASCADE);
		mniOptionsHeader.setText("&Options");	
		mniOptionsHeader.setMenu(mnuOptions);
		
		final MenuItem mniHelpHeader = new MenuItem(mnuMain, SWT.CASCADE);
		mniHelpHeader.setText("&Help");	
		mniHelpHeader.setMenu(mnuHelp);
	
		final MenuItem mniFileRestartSelServer = new MenuItem(mnuFile, SWT.PUSH);
		mniFileRestartSelServer.setText("Restart Selenium &Server");
		
		final MenuItem mniFileExit = new MenuItem(mnuFile, SWT.PUSH);
		mniFileExit.setText("E&xit");
		
		final MenuItem mniOptionsOptions = new MenuItem(mnuOptions, SWT.PUSH);
		mniOptionsOptions.setText("&Options...");
	
		mniFileExit.addSelectionListener(new SelectionListener()
		{
		    public void widgetSelected(SelectionEvent event)
		    {
		    	shMain.close();
				display.dispose();
		    }
	
		    public void widgetDefaultSelected(SelectionEvent event)
		    {
		    	System.out.println("WTF does 'widgetDefaultSelected(SelectionEvent event)' do?");
		    }
		});
		
		mniOptionsOptions.addSelectionListener(new SelectionListener()
		{
		    public void widgetSelected(SelectionEvent event)
		    {
		    	ConfigView swtConfigView = new ConfigView(app, shMain);
		    	final Shell shConfig = swtConfigView.initView();
		    	shConfig.open();
		    }
	
		    public void widgetDefaultSelected(SelectionEvent event)
		    {
		    	System.out.println("WTF does 'widgetDefaultSelected(SelectionEvent event)' do?");
		    }
		});
	
		shMain.setMenuBar(mnuMain);
	
		return shMain;
    }
}
